package javasec.contempt.visuals;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.awt.SWT_AWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Vector;

import javasec.contempt.data.SeedConnection;
import javasec.contempt.data.SeedConnector;
import javasec.contempt.dialogs.StringDialog;
import javasec.seed.SeedInterface;

import javax.sql.DataSource;


public class ChatVisual extends EditorPart
{
	public static String ID = "javasec.contempt.visuals.ChatVisual";
	//private Text entry;
	String target;
	private SeedConnection seedConnection=null;
	private SeedInterface teamSeed=null;
	private Text text=null;
	private Display display=null;
	private String nick=System.getProperty("user.name");
	
	public ChatVisual()
	{
		// TODO Auto-generated constructor stub
	}

	public void doSave(IProgressMonitor monitor)
	{
		// TODO Auto-generated method stub

	}

	public void doSaveAs()
	{
		// TODO Auto-generated method stub

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException
	{
		System.out.println("new database editor init()ed");
		// TODO Auto-generated method stub
		setSite(site);
		setInput(input);
		
		seedConnection=((VisualInput)input).seedConnection;
		setPartName(seedConnection.name+" chat");
	}

	public boolean isDirty()
	{
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isSaveAsAllowed()
	{
		// TODO Auto-generated method stub
		return false;
	}
	
	public void createPartControl(Composite parent)
	{		
		try
		{
			// set the team seed
			teamSeed=(SeedInterface)seedConnection.rmiRegistry.lookup("TeamSeed");

			// save display for async exec later
			display=Display.getCurrent();
			Composite verticalComposite=new Composite(parent,SWT.NONE);
			verticalComposite.setLayout(new GridLayout(1,false));
			
			// create the table
			text = new Text(verticalComposite,SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
			text.setLayoutData(new GridData(GridData.FILL_BOTH));
			
			seedConnection.notifier.subscribe("incomingMessage",this,"addMessage");
			
			final Composite chatEntry=new Composite(verticalComposite,SWT.NULL);
			chatEntry.setLayout(new GridLayout(2,false));
			chatEntry.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
			final Button nickButton=new Button(chatEntry,SWT.PUSH);
			nickButton.setText(nick);
			final Text entryText=new Text(chatEntry,SWT.NONE);
			entryText.setLayoutData(new GridData(GridData.FILL_BOTH));
			nickButton.pack();
			
			// set what happens when text is enteres
			entryText.addSelectionListener(new SelectionListener()
			{
				public void widgetDefaultSelected(SelectionEvent e)
				{
					System.out.println(entryText.getText());
					try
					{
						teamSeed.invokeMethod("sendMessage",new Object[]{nick+": "+entryText.getText()});
					}
					catch (RemoteException remoteException)
					{
						remoteException.printStackTrace();
					}
					entryText.setText("");
				}

				public void widgetSelected(SelectionEvent e)
				{
				}
				
			});
			
			nickButton.addSelectionListener(new SelectionListener()
			{
				public void widgetDefaultSelected(SelectionEvent e)
				{
				}

				public void widgetSelected(SelectionEvent e)
				{
					StringDialog dialog=new StringDialog(null,"set nick",new String[]{"nick"});
					dialog.open();
					if(dialog.aborted)return; // if user clicks cancel, quit
					
					nick=dialog.getValues()[0];
					nickButton.setText(nick);
					nickButton.pack();  // properly resize the button
					chatEntry.layout(); // make sure entryText is resized properly
				}
			});
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

	public void addMessage(Object o)
	{
		if(!(o instanceof String))return;
		final String message=o.toString();
		
		// add new table item (must be in display thread to do so)
		display.asyncExec(new Runnable()
		{
			public void run()
			{
				text.append(message+"\n");
			}
		});
	}
	
	public void setFocus()
	{
	}

}
